package src.first;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;

public class StringUtils {

    @SuppressWarnings("uncheck")
    public String toString(Object object){
        Class<?> aClass = object.getClass();
        StringBuilder builder = new StringBuilder();
        Package aPackage = aClass.getPackage();
        builder.append("包名："+aPackage.getName()+"\t");
        String simpleName = aClass.getSimpleName();
        builder.append("类名："+simpleName+"\n");
        builder.append("公共构造方法：\n");
        //获得所有代表构造方法的Constructor数组
        Constructor[] constructors = aClass.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            String modifier = Modifier.toString(constructor.getModifiers());
            if(modifier.contains("public")){
                builder.append(constructor.toGenericString()+"\n");
            }
        }
        builder.append("公共域:\n");
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            String string = Modifier.toString(declaredField.getModifiers());
            if(string.contains("public")){
                builder.append(declaredField.toGenericString()+"\n");
            }
        }
        builder.append("公共方法：\n");
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            String string = Modifier.toString(declaredMethod.getModifiers());
            if(string.contains("public")){
                builder.append(declaredMethod.toGenericString()+"\n");
            }
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new StringUtils().toString(new Date()));
    }
}
